package com.bridgeintelligent.tag.engine.mvc;

import com.bridgeintelligent.tag.engine.api.rsp.DimLastMonthRsp;
import com.bridgeintelligent.tag.engine.api.rsp.DimMonitorDetailRsp;
import com.bridgeintelligent.tag.engine.api.rsp.DimTop10Rsp;
import com.bridgeintelligent.tag.engine.es.model.DimMonitorDetailModel;
import com.bridgeintelligent.tag.engine.es.model.LastMonthModel;
import com.bridgeintelligent.tag.engine.es.model.Top10Model;
import com.bridgeintelligent.tag.engine.mvc.service.IDimMonitorService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @description
 * @author: grp 2020-10-12 10:20
 **/
@Api(tags = "标签历史监控模块")
@AllArgsConstructor
@RestController
@RequestMapping(value = "/api/engine/monitor")
@Slf4j
public class DimMonitorController {

    private IDimMonitorService monitorService;
    private ObjectMapper objectMapper;

    @ApiOperation(value = "上个月", notes = "标签监控")
    @PostMapping("/lastMonth")
    public DimLastMonthRsp lastMonth(String condition) throws IOException {
        LastMonthModel lastMonthModel = objectMapper.readValue(condition, LastMonthModel.class);
        long t = System.currentTimeMillis();
        DimLastMonthRsp rsp = monitorService.lastMonth(lastMonthModel);
        log.info(lastMonthModel.getDimId()+"-------lastMonth耗时----"+(System.currentTimeMillis()-t));
        return rsp;
    }
    @ApiOperation(value = "top10", notes = "标签监控")
    @PostMapping("/top10")
    public DimTop10Rsp top10(String condition) throws IOException {
        Top10Model top10Model = objectMapper.readValue(condition, Top10Model.class);
        long t1 = System.currentTimeMillis();
        DimTop10Rsp rsp = monitorService.top10(top10Model);
        log.info(top10Model.getDimId()+"-------top10耗时----"+(System.currentTimeMillis()-t1));
        return rsp;
    }
    @ApiOperation(value = "详情", notes = "标签监控")
    @PostMapping("/detail")
    public DimMonitorDetailRsp detail(String condition) throws IOException {
        DimMonitorDetailModel monitorDetailModel = objectMapper.readValue(condition, DimMonitorDetailModel.class);
        if(monitorDetailModel.getPageNumber() == null) {
            monitorDetailModel.setPageNumber(1);
        }
        if(monitorDetailModel.getPageSize() == null) {
            monitorDetailModel.setPageSize(10);
        }
        long t1 = System.currentTimeMillis();
        DimMonitorDetailRsp rsp = monitorService.detail(monitorDetailModel);
        log.info(monitorDetailModel.getDimId()+"-------detail耗时----"+(System.currentTimeMillis()-t1));
        return rsp;
    }
}
